package com.action;

import com.pojo.Subject;
import com.service.SubjectService;
import com.vo.ResponseBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/subject")
@CrossOrigin("*")
public class SubjectAction {
    @Autowired
    private SubjectService subjectService;

    /**
     * 查询所有任课科目
     *
     * @return
     */
    @RequestMapping("/findBySubjectId")
    public ResponseBean findAllSubjectBySubjectId() {
        // 查询已任课科目
        List<Subject> list = subjectService.findBySubjectId();
        if (list != null) {
            return new ResponseBean(200, 0, list);
        }
        return new ResponseBean(500, 0, null);
    }

    /**
     *
     * @param subject_id 指定 科目
     * @return xx
     * 邹益煌
     */
    @RequestMapping("/findAllSubjectBySubjectId")
    public ResponseBean findAllSubjectBySubjectId(String subject_id) {
        Map m = new HashMap();
        if (subject_id != null) {
            m.put("subject_id", subject_id);
        }
        List<Map<String, String>> map = subjectService.findAllSubjectBySubjectId(m);
        System.out.println("map.size()" + map.size());
        if (map.size() != 0) {
            return new ResponseBean(200, map.size(), map);
        }
        return new ResponseBean(500, 0, "查询失败,科目不存在");
    }

    /**
     * 添加科目
     * @param subject 科目参数
     * @return
     *
     */
    @RequestMapping("/addSubject")
    public ResponseBean addSubject(Subject subject){
        boolean f = subjectService.save(subject);
        return new ResponseBean(0,0,f);
    }

}
